package com.pic.mgt.haishin;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;

import com.pic.mgt.config.ParseXMLException;
import com.pic.mgt.config.XMLConfig;

public final class AppointedHaishinRuleConfig extends XMLConfig {
	private static AppointedHaishinRuleConfig appointedHaishinRuleConfig = null;
	
	private final String appointedConfigFile =
			"config/system/haishin/AppointedHaishin.conf.xml";
	private final String appointedConfigSchemaFile = 
			"config/system/haishin/AppointedHaishin.conf.xsd";
	
	private List<AppointedHaishinSizePattern> appointedHaishinPatternList;
	private int clientDivideAmount;
	
	private AppointedHaishinRuleConfig() throws ParseXMLException {
		super();
	}
	
	public synchronized static AppointedHaishinRuleConfig getInstance()
												throws ParseXMLException{
		if (appointedHaishinRuleConfig == null) {
			appointedHaishinRuleConfig = new AppointedHaishinRuleConfig();
		}
		return appointedHaishinRuleConfig;
	}
	
	@Override
	protected void loadConfig() throws ParseXMLException {
		if (this.appointedHaishinPatternList == null) {
			this.appointedHaishinPatternList
				= new LinkedList<AppointedHaishinSizePattern>();
		}	
		
		OMElement rootElement;		
		try{
			// 剖析並驗證(符合Schema)XML設定檔
			rootElement = super.parseXMLConfigFile(
					appointedConfigFile, appointedConfigSchemaFile);
		} catch (Exception e) {
			throw new ParseXMLException(
					"Load XML Config '" + appointedConfigFile + "' failed.", e);
		}
		// 讀取各設定值
		parseConfig(rootElement);
	}
	
	// 讀取大容量配信各設定值
	private void parseConfig(OMElement rootElement) {
		OMElement sizeDefineElement = 
			rootElement.getFirstChildWithName(new QName("SizeDefine"));				
	Iterator<OMElement> sizePatterns = 
		sizeDefineElement.getChildrenWithName(new QName("SizePattern"));
	while(sizePatterns.hasNext()){
		AppointedHaishinSizePattern sizePattern = new AppointedHaishinSizePattern();
		
		OMElement sizePatternElement = sizePatterns.next();
		OMElement fileSizeElement = sizePatternElement.
							getFirstChildWithName(new QName("FileSize"));
		OMElement minElement = fileSizeElement.getFirstChildWithName(new QName("MIN"));
		OMElement maxElement = fileSizeElement.getFirstChildWithName(new QName("MAX"));
		OMElement expectedDLTimeElement = sizePatternElement
										  .getFirstChildWithName(new QName("ExpectedDLTime"));
		OMAttribute sizePatternAttribute_Name = sizePatternElement
														.getAttribute(new QName("Name"));
		
		sizePattern.setExpectedDLTime(Integer.valueOf(expectedDLTimeElement.getText()));
		sizePattern.setLowerBound(Long.valueOf(minElement.getText()));
		sizePattern.setUpperBound(Long.valueOf(maxElement.getText()));
		sizePattern.setName(sizePatternAttribute_Name.getAttributeValue());
		
		appointedHaishinPatternList.add(sizePattern);
	}
	OMElement assignFixDivideAmountElement = rootElement
						.getFirstChildWithName(new QName("AssignFixDivideAmount"));
	OMElement valueElement = assignFixDivideAmountElement
											.getFirstChildWithName(new QName("Value"));
	clientDivideAmount = Integer.valueOf(valueElement.getText());	
	}
	
	// --- getter and setter 

	public List<AppointedHaishinSizePattern> getAppointedHaishinPatternList() {
		return appointedHaishinPatternList;
	}

	public int getClientDivideAmount() {
		return clientDivideAmount;
	}
}
